package com.linkstec.json;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkstec.dto.TemplateReplaceDto;

public class JsonUtil {
	private static final ObjectMapper om = new ObjectMapper();
	private static final Logger logger = LoggerFactory
			.getLogger(JsonUtil.class);

	public static String toJsonString(Object bean) {
		try {
			return om.writeValueAsString(bean);
		} catch (JsonProcessingException e) {
			logger.error("", e);
		}
		return null;
	}

	public static <T> T toBean(String jsonString, Class<T> beanClass) {
		try {
			return om.readValue(jsonString, beanClass);
		} catch (Exception e) {
			logger.error("", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> toMap(String jsonString) {
		try {
			Map<String, Object> m=om.readValue(jsonString, Map.class);
			if(m.size()<12){
				Map<String, Object> mm=new java.util.HashMap<String,Object>(m.size());
				for (Entry<String,Object> entry : m.entrySet()){
					mm.put(entry.getKey(), entry.getValue());
				}
				m=null;
				return mm;
				
			}else
				return m;
		} catch (Exception e) {
			logger.error("", e);
		}
		return null;
	}
	
	public static void main(String[] args) {
		Map<String, String> stockAnnMap = new LinkedHashMap<String, String>();
		stockAnnMap.put("STOCK_CODE", "600343");
		stockAnnMap.put("ANN_TITLE", "234航天动力”公布关于完成工商变更登记及“三证合一”的公告");
		stockAnnMap.put("ANN_CONTENT", "    (600343)“航天动力”公布关于完成工商变更登记及“三证合一”的公告\n\n    仅供参考，请查阅当日公告全文。\n\n    公告全文参考链接:\n    http://static.sse.com.cn/disclosure/listedinfo/announcement/c/2016-05-11/600343_20160511_1.pdf");
		stockAnnMap.put("FILE_LINK", "http://static.sse.com.cn/disclosure/listedinfo/announcement/c/2016-05-11/600343_20160511_1.pdf");
		
		
		String jsonStr = JsonUtil.toJsonString(stockAnnMap);
		
		long begintime = System.currentTimeMillis();
		for(int i = 0; i < 100000; i ++)
		{
			JsonUtil.toMap(jsonStr);
		}
		long endtime = System.currentTimeMillis();
		System.out.println("cost "+(endtime - begintime)+"ms");
		System.out.println("-------------华丽的分割线----------------------------------------------------------------------");
		
		Map<String, Object> templetInfo = new HashMap<String, Object>();
		templetInfo.put("businessNo", "businessNo");
		templetInfo.put("showWay", "showWay");
		templetInfo.put("infoTmpltId", 12345);
		templetInfo.put("infoContents", "infoContents");
		templetInfo.put("infoContentsSh", "infoContentsSh");
		templetInfo.put("infoTitle", "infoTitle");
		templetInfo.put("infoTitleSh", "infoTitleSh");
		templetInfo.put("bodyFormat", "bodyFormat");
		templetInfo.put("summary", "summary");
		templetInfo.put("sendAlias", "sendAlias");
		templetInfo.put("yesOrNo", "yesOrNo");
		templetInfo.put("cacheTime", "cacheTime");
		templetInfo.put("msgDetailType", null);//json比bean少不报错
		templetInfo.put("duochulaide", "duochulaide");// json比bean多报错com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
		//@JsonIgnoreProperties(ignoreUnknown = true) 用这个注解解决
		String templtStr = JsonUtil.toJsonString(templetInfo);
		logger.info(templtStr);
		TemplateReplaceDto templateReplaceDto = JsonUtil.toBean(templtStr, TemplateReplaceDto.class);
		logger.info(templateReplaceDto.toString());
		if (templateReplaceDto.getMsgDetailType() == null ){
			logger.info("为null");
		} else {
			logger.info("不为null");
		}
	}
}
